/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-03-20 19:12:04
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-05-13 17:50:10
 * @FilePath: \phoenix-client-damo\assets\src\Test.ts
 * @Description: 
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { AvatarModel } from "./models/avatar.model";
import PhoenixComponent from "./phoenix_client_sdk/PhoenixComponent";
import { PhoenixEventDefine } from "./phoenix_client_sdk/defines/event.define";
import { Account } from "./phoenix_client_sdk/entities/Account";
import { Avatar } from "./phoenix_client_sdk/entities/Avatar";
import * as sa from './cocoscreator-game.min.js'
// import * as crypto from "crypto";
// const CryptoJS = require("crypto-js");
const { ccclass, property } = cc._decorator;


@ccclass
export default class Test extends cc.Component {

    @property(cc.EditBox)
    editBoxAccount: cc.EditBox = null;

    @property(cc.EditBox)
    editBoxPassword: cc.EditBox = null;

    phoenixComponent: PhoenixComponent = null;

    start() {
        this.phoenixComponent = cc.find("Canvas/Phoenix")!.getComponent("PhoenixComponent")
        this.phoenixComponent!.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_SUCCESS, (recode) => {
            this.callBackLogin(recode);
        });
        this.phoenixComponent!.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_FAIL, (recode) => {
            this.callBackFailed(recode);
        });
        // sa.setPara({       
        //     server_url: 'http://127.0.0.1:8088/api/gp?project=clklogapp&token=5388ed7459ba4c4cad0c8693fb85630a',
        //     show_log: true
        // });
        // sa.init();
        // sa.login("88888");
    }

    async callBackLogin(recode) {
        console.log("callBackLogin");
        console.log(recode);
        console.log("登录成功");
        let accountIns = this.phoenixComponent!.getAccountIns() as Account;
        console.log("token为:" + accountIns.getToken());
        console.log("token过期时间为:" + accountIns.getTokenExpireTime());
        if (!this.phoenixComponent!.checkLoginHasAvatar()) {
            console.log("需要创建角色");
            recode = await this.phoenixComponent!.createAvatar<AvatarModel>(new AvatarModel());
            if (recode == 0) {
                console.log("创建角色成功");
            } else {
                console.log("创建角色失败=>" + recode);
            }
        } else {
            console.log("直接请求角色");
            recode = await this.phoenixComponent!.getAvatar<AvatarModel>();
            if (recode == 0) {
                console.log("获取角色成功");
                let avatar = this.phoenixComponent!.getAvatarIns() as Avatar<any>;
                let avatarData = avatar.getAvatarData() as AvatarModel;
                console.log("角色数据分数为:" + avatarData.gamePoint);
            } else {
                console.log("获取角色失败=>" + recode);
            }
        }
    }

    callBackFailed(recode) {
        console.log("callBackFailed");
        console.log(recode);
        console.log("登录失败");
    }

    async onClickQueryServerInfo() {
        console.log("onClickQueryServerInfo");
        this.phoenixComponent!.queryServerInfo();
    }

    async onClickLogin(e, data) {
        console.log("onClickLogin");
        this.phoenixComponent!.platformLogin(this.editBoxAccount.string, this.editBoxPassword.string);
    }

    async onClickUpdateAvatar(e, data) {
        let avatar = this.phoenixComponent!.getAvatarIns() as Avatar<any>;
        let avatarData = avatar.getAvatarData() as AvatarModel;
        avatarData.gamePoint = 100;
        let recode = await this.phoenixComponent!.updateAvatar();
        if (recode == 0) {
            console.log("更新角色成功");
        } else {
            console.log("更新角色失败=>" + recode);
        }
    }

    async onClickUpdateToken(e, data) {
        let recode = await this.phoenixComponent!.updateAccountToken();
        if (recode == 0) {
            console.log("更新token成功");
            let accountIns = this.phoenixComponent!.getAccountIns() as Account;
            console.log("token为:" + accountIns.getToken());
            console.log("token过期时间为:" + accountIns.getTokenExpireTime());
        } else {
            console.log("更新token失败=>" + recode);
        }
    }

    async onClickUpdateThird(e, data) {
        let recode = await this.phoenixComponent!.updateThirdAccountToken();
        if (recode == 0) {
            console.log("更新第三方token成功");
        } else {
            console.log("更新第三方token失败=>" + recode);
        }
    }

    async onClickActionReport(e,data){
        let recode = await this.phoenixComponent!.alipayReportAction("active", "gamecenter");
        if (recode == 0) {
            console.log("调用activeCode上报成功");
        } else {
            console.log("调用activeCode上报失败=>" + recode);
        }
    }

}
